package org.bluesky.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import com.qiniu.util.Json;
import org.bluesky.framework.ResponseResult;
import org.bluesky.framework.dto.CategoryDto;
import org.bluesky.framework.entity.Category;
import org.bluesky.framework.enums.AppHttpCodeEnum;
import org.bluesky.framework.service.CategoryService;
import org.bluesky.framework.utils.BeanCopyUtils;
import org.bluesky.framework.utils.WebUtils;
import org.bluesky.framework.vo.CategoryVo;
import org.bluesky.framework.vo.ExcelCategoryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

@RestController
@RequestMapping("/content/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @GetMapping("/listAllCategory")
    public ResponseResult listAllCategory(){
        List<CategoryVo> categoryVos = categoryService.listAllCategory();
        return ResponseResult.okResult(categoryVos);
    }

    @PreAuthorize("@ps.hasPermission('content:category:export')")
    @GetMapping("/export")
    public void exportDataToExcel(HttpServletResponse response){
        //下载请求头设置
        // 获取需要导出的数据
        // 把数据写入到Excel
        //出现异常响应json
        try {
            WebUtils.setDownLoadHeader("分类.xlsx",response);
            List<Category> list = categoryService.list();
            List<ExcelCategoryVo> excelCategoryVos = BeanCopyUtils.copyBeanList(list, ExcelCategoryVo.class);
            EasyExcel.write(response.getOutputStream(), ExcelCategoryVo.class).autoCloseStream(Boolean.FALSE).sheet("分类导出")
                    .doWrite(excelCategoryVos);
        } catch (Exception e) {
            //重置response
            response.reset();
            ResponseResult responseResult = ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR);
            WebUtils.renderString(response, JSON.toJSONString(responseResult));
        }
    }

    @GetMapping("/list")
    public ResponseResult list(CategoryDto categoryDto,Integer pageNum,Integer pageSize){
        return categoryService.getListByMessage(categoryDto,pageNum,pageSize);
    }

    @GetMapping("/{id}")
    public ResponseResult getCategoryById(@PathVariable("id")Long id){
        Category category = categoryService.getById(id);
        CategoryVo categoryVo = BeanCopyUtils.copyBean(category, CategoryVo.class);
        return ResponseResult.okResult(categoryVo);
    }

    @PostMapping
    public ResponseResult addCategory(@RequestBody CategoryDto categoryDto){
        Category category = BeanCopyUtils.copyBean(categoryDto, Category.class);
        categoryService.save(category);
        return ResponseResult.okResult();
    }

    @PutMapping
    public ResponseResult updateCategory(@RequestBody CategoryDto categoryDto){
        Category category = BeanCopyUtils.copyBean(categoryDto, Category.class);
        categoryService.updateById(category);
        return ResponseResult.okResult();
    }

    @DeleteMapping("/{id}")
    public ResponseResult deleteCategoryById(@PathVariable("id")Long id){
        categoryService.removeById(id);
        return ResponseResult.okResult();
    }
}
